#发布新帖页
import self
from selenium.webdriver import ActionChains

from common.com import ForumDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.common.exceptions import TimeoutException


class PostEdit:
    driver = ""
    url = ""
    def __init__(self):
        self.url = "http://127.0.0.1:58080/index.html"
        self.driver = ForumDriver.driver
        self.driver.get( self.url )


    def PostRelease(self):
        #点击发布帖子
        self.driver.find_element( By.CSS_SELECTOR,"#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post").click()
        # 2. 版块选择与验证（以选择“C++”版块为例）
        # 2.1 等待版块选择框加载完成并点击
        board_select = WebDriverWait( self.driver, 10 ).until(
            EC.element_to_be_clickable( (By.CSS_SELECTOR, "#article_post_borad") )  # 假设版块选择框的CSS选择器
        )
        board_select.click()
        # 2.2 选择“C++”版块（根据实际选项调整选择器）
        cpp_board = WebDriverWait( self.driver, 10 ).until(
            EC.element_to_be_clickable( (By.CSS_SELECTOR, "#article_post_borad > option:nth-child(2)") )
        )
        cpp_board.click()
        # 2.3 验证版块是否选中（检查选中的文本是否为“C++”）
        selected_board = self.driver.find_element( By.CSS_SELECTOR, "#article_post_borad > option:nth-child(2)" )
        assert selected_board.text == "C++"
        print( "版块选择验证通过：C++" )
        #标题
        self.driver.find_element( By.CSS_SELECTOR, "#article_post_title" ).send_keys( "自动化测试" )
        ForumDriver.getScreenShot()
        time.sleep( 3 )
        # 内容
        # 4. 内容模块：模拟键盘输入到CodeMirror编辑器
        # 4.1 等待编辑器加载并激活（定位CodeMirror可交互区域）
        content_editor = WebDriverWait( self.driver, 10 ).until(
            EC.element_to_be_clickable( (By.CSS_SELECTOR, "#edit-article > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div") )  # CodeMirror编辑器核心区域
        )
        # 4.2 模拟用户操作：点击激活编辑器→输入内容（用ActionChains确保焦点）
        actions = ActionChains( self.driver )
        actions.click( content_editor )  # 点击激活
        actions.send_keys( "自动化测试具体内容" )
        ForumDriver.getScreenShot()
        actions.perform()  # 执行操作
        print( "内容输入完成（模拟键盘输入）" )
        ForumDriver.getScreenShot()  # 截图记录内容
        # 滑动到底部，点击发布
        self.driver.execute_script( "window.scrollTo(0,document.body.scrollHeight)" )
        time.sleep( 1 )  # 短暂等待滚动完成
        submit_btn = WebDriverWait( self.driver, 10 ).until(
            EC.element_to_be_clickable( (By.CSS_SELECTOR, "#article_post_submit") )
        )
        submit_btn.click()
        print( "已点击发布按钮" )
        time.sleep( 3 )
        #屏幕截图
        ForumDriver.getScreenShot()
        # 检查新帖子是否发布成功
        try:
            # 等待弹窗出现（设置超时时间，如3秒，与弹窗自动消失时间匹配或稍短）
            alert = WebDriverWait( self.driver, 3 ).until( EC.visibility_of_element_located((By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div")) )
            # 获取弹窗文本并打印
            alert_text = alert.text
            print( f"弹窗内容: {alert_text}" )
        except TimeoutException:
            print( "等待弹窗超时，未检测到弹窗" )




